package com.xh.bussiness.textbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.teachbook.bean.dto.TeachingBookPracticeContentDto;
import com.xh.bussiness.teachbook.bean.dto.TeachingBookPracticeDto;
import com.xh.bussiness.teachbook.constant.TeachingBookConstant;
import com.xh.bussiness.textbook.bean.request.TextbookPracticeRequest;
import com.xh.bussiness.textbook.dao.TextbookPracticeContentMapper;
import com.xh.bussiness.textbook.dao.TextbookPracticeMapper;
import com.xh.bussiness.textbook.model.TextbookPractice;
import com.xh.bussiness.textbook.model.TextbookPracticeContent;
import com.xh.bussiness.textbook.service.ITextbookPracticeService;
import com.xh.bussiness.tiku.bean.dto.QuestionDto;
import com.xh.bussiness.tiku.service.IQuestionService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 讲义练习 服务实现类
 * </p>
 *
 * @author JQM
 * @since 2019-09-26
 */
@Service
public class TextbookPracticeServiceImpl extends BaseServiceImpl<TextbookPracticeMapper, TextbookPractice> implements ITextbookPracticeService {

    @Autowired
    private TextbookPracticeContentMapper textbookPracticeContentMapper;
    @Autowired
    private IQuestionService questionService;

    @Override
    public XhPage queryTextbookPracticePage(XhPage page, TextbookPracticeRequest bean) {
        QueryWrapper qw=new QueryWrapper<TextbookPractice>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryTextbookPractice(TextbookPracticeRequest bean) {
        QueryWrapper qw=new QueryWrapper<TextbookPractice>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }


    @Override
    public void deleteByTextbookId(String textbookId) {
        this.dao.deleteByTextbookId(textbookId);
    }

    @Override
    public TeachingBookPracticeDto getTextbookPracticeByTextbookId(String textbookId) {
        QueryWrapper<TextbookPractice> qw = new QueryWrapper<>();
        qw.eq("textbook_id", textbookId);
        TextbookPractice textbookPractice = this.dao.selectOne(qw);

        if (textbookPractice == null) {
            return null;
        }
        QueryWrapper<TextbookPracticeContent> contentQueryWrapper = new QueryWrapper<>();
        contentQueryWrapper.eq("practice_id", textbookPractice.getId());
        contentQueryWrapper.orderByAsc("order_no");
        List<TextbookPracticeContent> contents = textbookPracticeContentMapper.selectList(contentQueryWrapper);
        if (CollectionUtils.isEmpty(contents)) {
            return null;
        }

        List<TeachingBookPracticeContentDto> practiceContentDtos = new ArrayList<>();
        for (TextbookPracticeContent content : contents) {
            TeachingBookPracticeContentDto contentDto = new TeachingBookPracticeContentDto();
            contentDto.setId(content.getId());
            contentDto.setBlankLine(content.getBlankLine());
            contentDto.setContentType(content.getContentType());
            contentDto.setContent(content.getContent());
            contentDto.setScore(content.getScore());
            contentDto.setOrderNo(content.getOrderNo());

            if (content.getContentType().equals(TeachingBookConstant.PracticeContentType.QUESTION)) {
                QuestionDto question = questionService.getQuestion(content.getQuestionId(), null);
                contentDto.setQuestion(question);
                contentDto.setQuestionId(content.getQuestionId());
                contentDto.setSubject(content.getSubject());
            }
            practiceContentDtos.add(contentDto);
        }
        TeachingBookPracticeDto practiceDto = new TeachingBookPracticeDto();
        practiceDto.setId(textbookPractice.getId());
        practiceDto.setTeachingBookId(textbookPractice.getTextbookId());
        practiceDto.setTotalScore(textbookPractice.getTotalScore());
        practiceDto.setUseTime(textbookPractice.getUseTime());
        practiceDto.setContentDtos(practiceContentDtos);

        return practiceDto;
    }

    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(TextbookPracticeRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        return qw;
    }
}
